--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的派发

local skynet = require "skynet"
local log = require "Log"

local Message = class("Message")

function Message:ctor()
	self.svrName = ""	
	self.tbSelfMsg = {} --本服务要监听的消息
	self.tbOtherMsg = {} --别服务要监听的消息
end

--注册本服务里的消息
function Message:registerSelf(msgName,callback)
	self.tbSelfMsg[msgName] = callback
end

--别服务要监听的消息
function Message:registerOther(msgName,svrName)
	self.tbOtherMsg[msgName] = self.tbOtherMsg[msgName] or {}
	self.tbOtherMsg[msgName]["svrName"] = svrName 
end

--客户端消息派发
function Message:dispatchMessage( session, source, cmd, ... )
	local ok, res
	local func = self.tbSelfMsg[cmd] --
	if not func then 
		local svr = self.tbOtherMsg[cmd]
		if svr then
			res = skynet.call(svr,'lua', cmd, ...)
			skynet.retpack(res)
			return
		end	
		log.error("####### cmd "..cmd .." not found at globalsvr "..self.svrName)
		return
	end
	skynet.retpack(xx_pcall(func, ...))
end

--取dispatchMessage地址
function Message:dispatch()
	return handler(self,self.dispatchMessage)
end


return Message